//
//  ReceiptAddress.swift
//  QCFM2
//
//  Created by iMac on 15/12/28.
//  Copyright © 2015年 kuai. All rights reserved.
//


import Foundation
import UIKit
import Alamofire
import SwiftyJSON
import KeychainSwift

class ReceiptAddress: UIViewController,UITableViewDataSource, UITableViewDelegate, HttpProtocol {
    
    var currentIndex:NSIndexPath?
    
    var eHttp:HTTPController = HTTPController()
    
    @IBOutlet weak var AddressTable: UITableView!
    var addressList = [ReAdressModel]()
    var tableData:[JSON]=[]
    
    //从沙盒中获取数据
    let keychain = KeychainSwift(keyPrefix: "myTestKey_")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "管理收货地址"
        
        //self.tabBarController!.tabBar.hidden = true;
        //请求收货地址
        eHttp.delegate = self
        let getAddresUrl = "http://42.62.18.210:8090/apiradio/ar/getAddrs?userID=" + keychain.get("registerUserID")!+"&page=1"
        print(getAddresUrl)
        
        eHttp.onsearch(getAddresUrl)
        
    }
    
    /* 获取数据的回调函数 */
    func didRecieveResults(results: AnyObject) {
        //print("results: \(results)")
        let json = JSON(results)
        if let channels = json["obj"].array {
            
            print(channels)
            tableData = channels
            //print(tableData.count)
            
            for var i = 0; i < tableData.count; i++
            {
                let rowData:JSON = tableData[i]
                // print(rowData["channelName"].string)
                
                //recipes.append(Recipe(channelID: String(rowData["channelID"]), englishName: rowData["englishName"].string!, channelTopic: rowData["channelTopic"].string!, channelName: rowData["channelName"].string!))
                //addressList.append(ReAdressModel(Addressee: String(rowData["name"]), Cellphone: String(rowData["phoneNumber"]), ReceiptAddress: String(rowData["address"])))
                addressList.append(ReAdressModel(Addressee: String(rowData["name"]), Cellphone: String(rowData["phoneNumber"]), ReceiptAddress: String(rowData["address"]), ImageUlr: "weixuan"))
                
            }
            
            self.AddressTable!.reloadData()
        }
        
    }
    
    // MARK: - UITableView DataSource Methods
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identifier: String = "ReAdressCell"
        
        var cell: ReAdressModelList! = tableView.dequeueReusableCellWithIdentifier(identifier) as? ReAdressModelList
        
        if cell == nil {
            cell = ReAdressModelList(style: UITableViewCellStyle.Value1, reuseIdentifier: identifier)
        }
        
        cell.configurateTheCell(addressList[indexPath.row])
        
        return cell!
    }
    
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(addressList.count)
        return addressList.count
    }
    
    
    //MARK: - UITableView Delegate Method
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //tableView.deselectRowAtIndexPath(indexPath, animated: true)
        print("get")
        if currentIndex == nil {
            
        }else {
            addressList[(currentIndex?.row)!].ImageUrlStr = "weixuan"
        }
        currentIndex = indexPath
        addressList[indexPath.row].ImageUrlStr = "xuanzhong"
        self.AddressTable!.reloadData()
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

